<?php
/**
 * 
 * @author Freezy
 *
 */
class Propinsi extends MY_Controller {

	function Propinsi(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('departemen_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array('view'=>'template/horz_nav', 
											'view_data'=>array('data'=>$CONF['nav']['psdm']));
		$layoutConf['right_content'] = array('view'=>'propinsi/overview', 
											'view_data'=>$CONF['search']);
		$this->initialize_layout($layoutConf);
		
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'] = array_merge(
			$pageConf['jscript_default'],
			array('psdm/propinsi'));
		$pageConf['jscript'] = $pageConf['jscript_default'];
		
//		$pageConf['css_default'] = array_merge($pageConf['css_default'], array('datepicker'));
		$pageConf['css'] = $pageConf['css_default'];
		
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}
	
	function _search(){
		$this->load->model('propinsi_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$table_content = "";
		$row = 0;
		$data = $this->propinsi_model->select_last_entry($key, '', $limit, $offset, $orderby);
		$table_content = $this->load->view('propinsi/table_content', array('data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->propinsi_model->count_record()
		));
	}
	
	function _addModal(){
		$this->load->helper(array('html','url'));
		$this->finalize_view_str(
			$this->load->view('propinsi/add',null,true)
		);
	}
	
	function _save(){
		$this->load->model('propinsi_model');
		$inp = $this->input->post('inp');
		
		//save
		$res = $this->propinsi_model->insert( array(
			'propinsi_id' => $inp[0],
			'propinsi_nama' => $inp[1]
		));
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->propinsi_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _editModal($dep_id){
		$this->load->model('propinsi_model');
		
		$result = $this->propinsi_model->select_by_id($dep_id);
		$result = $result->row();
		$arr = array(
			'propinsi_id'=>$result->propinsi_id,
			'propinsi_nama'=>$result->propinsi_nama
		);
		
		$this->finalize_view_str(
			$this->load->view('propinsi/edit', $arr,true)
		);
	}
	
	function _update(){
		$this->load->model('propinsi_model');
		$inp = $this->input->post('inp');
		
		//save
		$ufield = array('propinsi_nama' => $inp[1]);
		$res = $this->propinsi_model->update(array('propinsi_id' => $inp[0]), $ufield );
		$msg = $res? "Proses update selesai" : "Proses update gagal, ".$this->propinsi_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _delete($id){
		$this->load->model('propinsi_model');
		$res = $this->propinsi_model->delete(array('propinsi_id' => $id));
		$msg = $res? "Proses hapus selesai" : "Proses hapus gagal, ".$this->propinsi_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	
	/***********************
	 * MISC
	 **********************/

	function _validateID($id){
		$this->load->model('propinsi_model');
		$check = $this->propinsi_model->select_by_id($id);
		if ($check = (array)$check->row()){
			echo json_encode(array('error'=>true,'msg'=>"ID telah digunakan oleh Propinsi '".$check['propinsi_nama']));
		}
		else{
			echo json_encode(array('error'=>false,'msg'=>""));
		}
	}
}
/* End of file psdm/departemen.php */
/* Location: ./system/application/controllers/psdm/departemen.php */